package com.googlecode.allinclusive.tools.srcgen.database;

import java.util.ArrayList;
import java.util.List;

/**
 * 表定义对象
 * 
 * @author xiayg
 */
public class TableDefinition {

    private String catalog;
    
    private String schema;
    
    private String tableName;
    
    private String description;
    
    private String javaClass;

    private ColumnDefinitions columnDefinitions = new ColumnDefinitions();
    
    public TableDefinition(String catalog, String schema, String tableName) {
    	this.catalog = catalog;
    	this.schema = schema;
    	this.tableName = tableName;
    }
    
	public ColumnDefinitions getColumnDefinitions() {
		return columnDefinitions;
	}

	public void setColumnDefinitions(ColumnDefinitions columnDefinitions) {
		this.columnDefinitions = columnDefinitions;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getJavaClass() {
		return javaClass;
	}

	public void setJavaClass(String javaClass) {
		this.javaClass = javaClass;
	}

	public String getCatalog() {
		return catalog;
	}

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}
    
    public List<String> getImportClasses() {
    	List<String> importClasses = new ArrayList<String>();
    	for (ColumnDefinition cd : columnDefinitions.getBaseColumns()) {
    		if (cd.getJavaTypeResolver().isExplicitlyImported()) {
    			if (!importClasses.contains(cd.getJavaTypeResolver().getFullyQualifiedName())) {
    				importClasses.add(cd.getJavaTypeResolver().getFullyQualifiedName());
    			}
    		}
    	}
    	return importClasses;
    }
    
    public List<String> getPrimaryImportClasses() {
    	List<String> importClasses = new ArrayList<String>();
    	for (ColumnDefinition cd : columnDefinitions.getPrimaryKeyColumns()) {
    		if (cd.getJavaTypeResolver().isExplicitlyImported()) {
    			if (!importClasses.contains(cd.getJavaTypeResolver().getFullyQualifiedName())) {
    				importClasses.add(cd.getJavaTypeResolver().getFullyQualifiedName());
    			}
    		}
    	}
    	return importClasses;
    }
    
    public String getPrimaryKeyArgs() {
    	StringBuilder sb = new StringBuilder();
    	
    	for (int i = 0; i < columnDefinitions.getPrimaryKeyColumns().size(); i++) {
    		ColumnDefinition cd = columnDefinitions.getPrimaryKeyColumns().get(i);
    		sb.append(cd.getJavaTypeResolver().getBaseSimpleName());
    		sb.append(" ");
    		sb.append(cd.getJavaProperty());
    		if (i == columnDefinitions.getPrimaryKeyColumns().size() - 1) {
    		} else {
    			sb.append(",");
    			sb.append("\r\n");
    			sb.append("\t\t\t");    			
    		}
    	}
    	
    	return sb.toString();
    }
}
